package org.mspring.mlog.web.admin;

import org.mspring.mlog.web.BaseAdminController;
import org.mspring.mlog.entity.LinkEntity;
import org.mspring.mlog.entity.UserEntity;
import org.mspring.mlog.service.LinkEntityService;
import org.mspring.mlog.support.TabItem;
import org.mspring.nbee.common.PageResult;
import org.mspring.nbee.db.dao.sql.builder.part.condition.Operator;
import org.mspring.nbee.db.dao.sql.builder.part.condition.SqlCondition;
import org.mspring.nbee.db.dao.sql.pager.OrderType;
import org.mspring.nbee.db.dao.sql.pager.SqlPager;
import org.mspring.nbee.web.support.QueryParam;
import org.mspring.nbee.web.support.WebContext;
import org.mspring.nbee.web.view.json.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @author Gao Youbo
 * @since 2015-01-24 13:13
 */
@Controller
@RequestMapping("/admin/link")
@TabItem(item = "200115")
public class LinkController extends BaseAdminController {
    private LinkEntityService linkEntityService;

    @RequestMapping("/list")
    @TabItem(item = "200115005")
    public String list(WebContext context, Model model) {
        SqlPager pager = context.getPager();
        pager.addOrder("id", OrderType.DESC);
        List<SqlCondition> conditions = context.buildSqlConditions(
                model,
                new QueryParam(LinkEntity.Fields.name, Operator.LIKE)
        );
        PageResult<LinkEntity> result = linkEntityService.list(conditions, pager);
        model.addAttribute("result", result);
        return "/admin/link/list";
    }

    @RequestMapping("/create")
    @TabItem(item = "200115010")
    public String create(WebContext context, Model model) {
        return "/admin/link/create";
    }

    @RequestMapping("/create/do")
    public String doCreate(WebContext context, Model model) {
        UserEntity currentUser = getCurrentUser();
        LinkEntity link = context.fromRequest(LinkEntity.class);
        link.setCreateTime(new Date());
        link.setCreateUser(currentUser.getId());
        link.setCreateUserName(currentUser.getNickname());
        link.setUpdateTime(new Date());
        link.setUpdateUser(currentUser.getId());
        link.setUpdateUserName(currentUser.getNickname());
        linkEntityService.insert(link);
        return "redirect:/admin/link/list";
    }

    @RequestMapping("/edit")
    @TabItem(item = "200115015")
    public String edit(WebContext context, Model model) {
        Long id = context.getRequiredLong("id");
        LinkEntity link = linkEntityService.findById(id);
        model.addAttribute("link", link);
        return "/admin/link/edit";
    }

    @RequestMapping("/edit/do")
    public String doEdit(WebContext context, Model model) {
        UserEntity currentUser = getCurrentUser();
        long id = context.getRequiredLong("id");
        LinkEntity link = linkEntityService.findById(id);
        link = context.fromRequest(link);
        link.setUpdateTime(new Date());
        link.setUpdateUser(currentUser.getId());
        link.setUpdateUserName(currentUser.getNickname());
        linkEntityService.update(link);
        return "redirect:/admin/link/list";
    }

    @RequestMapping("/delete/do")
    @ResponseBody
    public JsonResult delete(WebContext context, Model model) {
        long id = context.getRequiredLong("id");
        boolean flag = linkEntityService.deleteById(id);
        return JsonResult.getJsonResult(flag);
    }

    @Autowired
    public void setLinkEntityService(LinkEntityService linkEntityService) {
        this.linkEntityService = linkEntityService;
    }
}
